package com.example.back.controller;


import com.example.back.model.Boardgame;
import com.example.back.model.Order;
import com.example.back.model.OrderProduct;
import com.example.back.model.SelectProduct;
import com.example.back.service.OrderService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单控制层
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    private static final Logger logger = LogManager.getLogger(OrderController.class);

    @Resource
    private OrderService orderService;

    @RequestMapping("/submitOrder")
    public Boolean submitOrder(@RequestParam("account") String account, @RequestParam("room") String room, @RequestParam("jsonString") String jsonString){
        logger.info("account: " + account + ";room: " + room);
        logger.info("jsonString: " + jsonString);
        int orderID = orderService.findOrderID(account, room);
        if(orderID == 0){
            orderService.addOrder(account, room);
            orderID = orderService.findOrderID(account, room);
        }
        ObjectMapper mapper = new ObjectMapper();
        try {
            double cost = 0;
            List<SelectProduct> productList = mapper.readValue(jsonString, new TypeReference<List<SelectProduct>>(){});
            for (SelectProduct product : productList){
                logger.info("id:  " + product.getId() + ";count: " + product.getNumber() + ";price: " + product.getPrice());
                int productID = product.getId();
                int count = product.getNumber();
                double price = product.getNumber() * product.getPrice();
                cost = cost + price;
                if(orderService.findProduct(orderID, productID))orderService.updateProduct(orderID, productID, count, price);
                else orderService.addProduct(orderID, productID, count, price);
            }
            orderService.addCost(orderID , cost);
            return true;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return false;
    }

    @RequestMapping("/getOrderList")
    @ResponseBody
    public List<Order> getOrderList(@RequestParam("account") String account){
        List<Order> orderList = orderService.findOrder(account);
        return orderList;
    }

    @RequestMapping("/getOrderProductList")
    @ResponseBody
    public List<OrderProduct> getOrderProductList(@RequestParam("orderID") int orderID){
        List<OrderProduct> orderProductList = orderService.findOrderProduct(orderID);
        return orderProductList;
    }

    @RequestMapping("/deleteOrder")
    public String deleteOrder(int orderID) {
        String rs = "";
        if (orderService.deleteOrder(orderID)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>订单删除成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>订单删除失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("finishOrder")
    public String finishOrder(int orderID) {
        String rs = "";
        if (orderService.finishOrder(orderID)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>订单状态更新成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>订单状态更新失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }
}
